package demo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.MultipartConfigElement;

import org.h2.server.web.WebServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import demo.bean.LifeBean;
import demo.filter.TimeLogFilter;
import demo.service.HelloService;
import spring.MyImport;
import spring.MyImport2;
import util.StatusUtil;


@EnableAsync
@RestController
@SpringBootApplication
@Import({simple.Hello.class,MyImport.class,MyImport2.class})
// 不指定 扫描路径 否认只扫描 当前包 ，否则要 写上所有包
// @ComponentScan({"inject"})  不会扫描 demo 及 子包
@ComponentScan({"inject","demo","comm"})
@ImportResource("classpath:beans.xml")
@ServletComponentScan("demo.filter")
@PropertySource(value = "classpath:config.properties", ignoreResourceNotFound = true,encoding = "UTF-8" )
public class DemoApp{
	
	// 默认只扫描当前包(含子包)， 引入其他配置类 使用 @Import 注解
	
	@RequestMapping("/demohello")
	public String hello() {
		return "hello springboot demo,"+new Date();
	}
	
	@Bean("helloService2")
	public HelloService helloService(){
		return new HelloService();
	}
	
	
	@Bean
	MultipartConfigElement multipartConfigElement() {
	    MultipartConfigFactory factory = new MultipartConfigFactory();
	    factory.setLocation("/tmp");
	    return factory.createMultipartConfig();
	}
	


	
	@Bean
    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
        registrationBean.addUrlMappings("/h2-console/*");
        return registrationBean;
    }
	
	@Bean
	public FilterRegistrationBean registFilter() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new TimeLogFilter());
		registration.addUrlPatterns("/*");
		registration.setName("TimeLogFilter");
		registration.setOrder(1);
		return registration;
	}

	public static void main(String[] args) {
		StatusUtil.put("args", args);
		
		// SpringApplication.run(DemoApp.class, args);
		SpringApplication application = new SpringApplication(DemoApp.class);
		
		Map<String,Object> defaultPropertiesMap = new HashMap<>();
		defaultPropertiesMap.put("a","1");
		application.setDefaultProperties(defaultPropertiesMap);
		
		Properties defaultProperties = new Properties();
		defaultProperties.put("b", 2);
		application.setDefaultProperties(defaultProperties);

		
		application.addInitializers(new MyApplicationContextInitializer());
		ApplicationContext ctx = application.run(args);
		
		System.out.println("ctx="+ctx);
		
		HelloService helloService = (HelloService)ctx.getBean("helloService");
		System.out.println(helloService.hello("dyyx"));
		
		LifeBean lifeBean = (LifeBean)ctx.getBean("lifeBean");
		System.out.println("lifeBean="+lifeBean);


		
	}
	
}